CommonUtils.regNamespace("com.im.constantManage");

com.im.constantManage.invoke = function () {
    var param = {"pageInfo":{"pageSize":20,"pageNumber":1}};
    var operType;
    var operValType;
    var constantobj;
    var constantvalobj;
    var tmpVals;
    var statusCd;
    var client = new com.im.common.ajax();
    var busicolumns = [[
        {field:'checkRow',title:"选择",width:100,fitColumns:true,checkbox : true  },
        {
            field: 'constParamId', title: "序号",width:$(this).width()*0.03, fitColumns: true,
            formatter: function (value, row, index) {
                return (index + 1);
            }
        },
        {field: 'constParamCode', title: "常量编码",width:$(this).width()*0.2, fitColumns: true},
        {field: 'constParamName', title: "常量名称",width:$(this).width()*0.2, fitColumns: true},
        {field: 'description', title: "常量描述",width:$(this).width()*0.25, fitColumns: true},
        {field: 'statusName', title: "状态",width:$(this).width()*0.1, fitColumns: true},
        {
            field: 'operation', title: "操作",width:$(this).width()*0.14, fitColumns: true,
            formatter: function (value, row, index) {
                return '<a href="javascript:;" onclick="com.im.constantManage.invoke.modifyBusiParam(\'' + index + '\',\'query\')" title="查看"><i class="fa fa-eye"></i></a>' +
                    '<a href="javascript:;" onclick="com.im.constantManage.invoke.modifyBusiParam(\'' + index + '\',\'modify\')" title="修改"><i class="fa fa-edit"></i></a>' +
                    '<!--<a href="javascript:;" onclick="com.im.constantManage.invoke.modifyBusiParam(\'' + index + '\',\'del\')" title="删除"><i class="fa fa-trash"></i></a>-->';
            }
        }
    ]];
    var busivalcolumns = [[
        {
            field: 'paramValId', title: "序号",width:$(this).width()*0.05, fitColumns: true,
            formatter: function (value, row, index) {
                return (index + 1);
            }
        },
        {field: 'paramValValue', title: "常量值",width:$(this).width()*0.15, fitColumns: true},
        {field: 'paramValName', title: "常量值名称",width:$(this).width()*0.1, fitColumns: true},
        {field: 'description', title: "常量值描述",width:$(this).width()*0.15, fitColumns: true},
        {
            field: 'operation', title: "操作",width:$(this).width()*0.1, fitColumns: true,
            formatter: function (value, row, index) {
                if(operType == 'add' || operType == 'modify'){
                    return '<a href="javascript:;" onclick="com.im.constantManage.invoke.modifyBusiParamVal(\'' + index + '\',\'modify\')" title="修改"><i class="fa fa-edit"></i></a>' +
                        '<a href="javascript:;" onclick="com.im.constantManage.invoke.modifyBusiParamVal(\'' + index + '\',\'del\')" title="删除"><i class="fa fa-trash"></i></a>';
                }else{
                    return '不可操作';
                }
            }
        }
    ]];
    function _init() {

        //加载一页
        _queryBusiParamPage();

        //绑定事件
        //查询
        $("#queryBtn").click(function () {
             param.constParamName = $("#constParamName").val();
             param.pageInfo.pageNumber = 1;
            _queryBusiParamPage();
        });
        //新增
        $("#addBtn").click(function(){
            tmpVals = [];
            $("#constParamCodeTxt").val("");
            $("#constParamNameTxt").val("");
            $("#decriptionTxt").val("");
            _modifyBusiParam(null,"add");
        });
        //保存
        //直接调用show看动态加载模态框不能显示出来
        $('#addEditModal').on('shown.bs.modal', function () {
            _showBusiParam();
        });
        //增加常量值
        $("#addValBtn").click(function(){
            //增加前先保存
            //_saveOrUpdateBusi();
            _modifyBusiParamVal(null,'add');
        });
        $("#saveBtn").click(function () {
            _saveOrUpdateBusi();
        });
        //
        $("#saveValBtn").click(function(){
            _saveOrUpdateBusiVal();
        });
        //
        $("#batchDelBtn").click(function () {
            _batchUpadateOrg("batchdel");
        });
        //用户状态
        $('#statusCdTxt').switchbutton({
            checked: true,
            onChange: function(checked){
                if (checked == true){
                    statusCd = '10';
                    param.statusCd = statusCd;
                    param.pageInfo.pageNumber =1;
                    _queryBusiParamPage();
                }
                if (checked == false) {
                    statusCd = '20';
                    param.statusCd = statusCd;
                    param.pageInfo.pageNumber =1;
                    _queryBusiParamPage();
                }}
        });
    }
    //flag true是否关闭展现
    function _saveOrUpdateBusi(){
        if(operType == 'query'){
            $("#addEditModal").modal("hide");
            return;
        }

        var constParamCode = $("#constParamCodeTxt").val();
        if(constParamCode == undefined || constParamCode == ''){
            toastr.error("请填写常量编码！");
            return;
        }
        var constParamName = $("#constParamNameTxt").val();
        if(constParamName == undefined || constParamName == ''){
            toastr.error("请填写常量名称！");
            return;
        }
        var description = $("#decriptionTxt").val();

        var sparam = {};
        var busiConstParam = {};
        busiConstParam.constParamCode = constParamCode;
        busiConstParam.constParamName = constParamName;
        busiConstParam.description = description;
        busiConstParam.operType = operType;

        if(operType != 'add'){
            busiConstParam.constParamId = constantobj.constParamId;
        }else{
            sparam.busiConstParamVals = tmpVals;
        }
        sparam.busiConstParam = busiConstParam;

        var result = client.callAsJson("commonService/saveOrUpdateBusiParam", sparam);
        if (result.success) {
            //这是新增保存按钮触发
            $("#addEditModal").modal("hide");
            //重新加载
            _queryBusiParamPage();
            toastr.info("操作成功！");
        }else{
            toastr.error("服务器忙，请稍后再试！");
        }
    };

    function _saveOrUpdateBusiVal(){

        var sparam = {};
        var busiConstParamVals = [];

        var paramValValue = $("#paramValValueTxt").val();
        var paramValName = $("#paramValNameTxt").val();
        if(paramValValue == undefined || paramValValue == ''){
            toastr.error("请填写常量值！");
            return;
        }
        if(paramValName == undefined || paramValName == ''){
            toastr.error("请填写常量值名称！");
            return;
        }

        var busiConstParamVal = {};
        busiConstParamVal.paramValValue = paramValValue;
        busiConstParamVal.paramValName = paramValName;
        busiConstParamVal.description = $("#valDescriptionTxt").val();

        if(operType == 'add'){
            busiConstParamVal.operType = operValType;

            if(operValType == 'add'){
                //判断当前临时数组常量值是否存在
                var ifexist = false;
                $.each(tmpVals,function (i,obj) {
                    if(obj.paramValValue == busiConstParamVal.paramValValue){
                        ifexist = true;
                        return ;
                    }
                });
                if(ifexist){
                    toastr.error("当前常量值已存在，请重新输入！");
                    return;
                }
                busiConstParamVal.paramValId = tmpVals.length;
                tmpVals.push(busiConstParamVal);
            }else if(operValType == 'modify'){
                //修改后的常量值是否存在
                var ifexist = false;
                $.each(tmpVals,function (i,obj) {
                    if(obj.paramValValue == busiConstParamVal.paramValValue
                        && obj.paramValId != constantvalobj.paramValId){
                        ifexist = true;
                        return ;
                    }
                });
                if(ifexist){
                    toastr.error("当前常量值已存在，请重新输入！");
                    return;
                }

                $.each(tmpVals,function (i,obj) {
                    if(obj.paramValId == constantvalobj.paramValId){
                        //替换成新值
                        tmpVals[i].paramValValue = busiConstParamVal.paramValValue;
                        tmpVals[i].paramValName = busiConstParamVal.paramValName;
                        tmpVals[i].description =busiConstParamVal.description;
                        return ;
                    }
                });

            }else{
                //del
                var tmp = [];
                $.each(tmpVals,function (i,obj) {
                    if(obj.paramValId == constantvalobj.paramValId){
                        //替换成新值
                        return ;
                    }else{
                        obj.paramValId = tmp.length;
                        tmp.push(obj);
                    }
                });
                //替换；
                tmpVals =  tmp;
            }
            $("#busiParamValList").datagrid({
                columns: busivalcolumns,
                data: tmpVals
            });
            $("#EditConstantModal").modal("hide");

        }else{

            busiConstParamVal.constParamId = constantobj.constParamId;
            busiConstParamVal.operType = operValType;
            if(operValType != 'add'){
                busiConstParamVal.paramValId = constantvalobj.paramValId;
            }
            busiConstParamVals.push(busiConstParamVal);

            sparam.busiConstParamVals = busiConstParamVals;

            if(paramValValue == undefined || paramValValue == ''){
                toastr.error("请填写常量值！");
                return;
            }
            if(paramValName == undefined || paramValName == ''){
                toastr.error("请填写常量值名称！");
                return;
            }
            var result = client.callAsJson("commonService/saveOrUpdateBusiParam", sparam);
            if (result.success) {
                $("#EditConstantModal").modal("hide");
                //重新加载列表
                var result = client.callAsJson("commonService/queryBusiConstValByCond", {"constParamId":constantobj.constParamId});
                if (result.success) {
                    $("#busiParamValList").datagrid({
                        columns: busivalcolumns,
                        data: result.result
                    });
                }
                toastr.info("操作成功！");
            }else{
                toastr.error("服务器忙，请稍后再试！");
            }
        }
    };
    function _modifyBusiParam(idx,type){
        constantobj = $('#busiList').datagrid('getData').rows[idx];
        operType = type;

        if(type == 'query'){
            $("#myModalLabel").html("查看常量");
        }
        if(type == 'del'){
            $("#myModalLabel").html("删除常量");
        }
        if(type == 'modify'){
            $("#myModalLabel").html("更新常量");
        }
        if(type  == 'add'){
            $("#myModalLabel").html("增加常量");
        }
        $("#addEditModal").modal("show");
    };

    //vflag是否可修改，aflag是否需要显示值
    function _showBusiParam(){


        if(operType == 'add'){
            $("#busiParamValList").datagrid({
                columns: busivalcolumns,
                data: []
            });
        }else{
            $("#constParamCodeTxt").val(constantobj.constParamCode);
            $("#constParamNameTxt").val(constantobj.constParamName);
            $("#decriptionTxt").val(constantobj.description);
            var result = client.callAsJson("commonService/queryBusiConstValByCond", {"constParamId":constantobj.constParamId});
            if (result.success) {
                $("#busiParamValList").datagrid({
                    columns: busivalcolumns,
                    data: result.result
                });
            }
        }

        if(operType == 'add' || operType == 'modify'){
            $("#constParamCodeTxt").removeAttr("disabled");
            $("#constParamNameTxt").removeAttr("disabled");
            $("#decriptionTxt").removeAttr("disabled");
            $("#addValBtn").removeAttr("disabled");
            $("#addValBtn").show();
            if(operType == 'modify'){
                $("#constParamCodeTxt").attr("disabled","disabled");
            }

        }else{
            $("#constParamCodeTxt").attr("disabled","disabled");
            $("#constParamNameTxt").attr("disabled","disabled");
            $("#decriptionTxt").attr("disabled","disabled");
            $("#addValBtn").attr("disabled","disabled");
            $("#addValBtn").hide();
        }

    };
    function _modifyBusiParamVal(idx,type){

        constantvalobj = $('#busiParamValList').datagrid('getData').rows[idx];
        $("#EditConstantModal").modal("show");
        operValType = type;
        if(type == 'modify'){
            $("#myModalLabel1").html("修改常量值");
        }
        if(type == 'del'){
            $("#myModalLabel1").html("删除常量值");
        }
        if(type == 'add'){
            $("#myModalLabel1").html("新增常量值");

        }
        if(type == 'add'){
            $("#paramValValueTxt").val('');
            $("#paramValNameTxt").val('');
            $("#valDescriptionTxt").val('');

        }else{
            $("#paramValValueTxt").val(constantvalobj.paramValValue);
            $("#paramValNameTxt").val(constantvalobj.paramValName);
            $("#valDescriptionTxt").val(constantvalobj.description);
        }
        if(type == 'del'){
            $("#paramValValueTxt").attr("disabled","disabled");
            $("#paramValNameTxt").attr("disabled","disabled");
            $("#valDescriptionTxt").attr("disabled","disabled");
        }else{
            $("#paramValValueTxt").removeAttr("disabled");
            if(type == 'modify'){
                $("#paramValValueTxt").attr("disabled","disabled");
            }
            $("#paramValNameTxt").removeAttr("disabled");
            $("#valDescriptionTxt").removeAttr("disabled");
        }

    };
    function _queryBusiParamPage(){

        var result = client.callAsJson("commonService/queryBusiConstByCond", param);
        if (result.success) {
            $("#busiList").datagrid({
        	    nowrap:false,
        	    fitColumns:true,
                columns: busicolumns,
                data: result.result
            });

            //页码
            $("#page").pagination({
                total : result.result.total,
                pageSize : result.result.pageSize,
                pageNumber : result.result.pageNumber,
                pageList : [20 , 40],
                beforePageText:  '第' ,
                afterPageText: '页 共'+result.result.totalPages+'页',
                displayMsg: '共 '+result.result.total+'条记录',
                onSelectPage: function (pageNumber, pageSize) {
                    param.pageInfo.pageSize = pageSize;
                    param.pageInfo.pageNumber = pageNumber;
                    _queryBusiParamPage(pageNumber,pageSize);
                }
            });
        } else {
            toastr.error("服务器忙！请稍后再试！" + result.resultMsg);
        }

    };

    function _batchUpadateOrg(type){

        var nowlist = $("#busiList").datagrid('getChecked');
        if(nowlist != undefined && nowlist.length > 0){
            var msgStr = "确认批量失效"+nowlist.length+"个常量么？";
            if(statusCd == '20'){
                type = "batcheff";
                var msgStr = "确认批量生效"+nowlist.length+"个常量么？";
            }
            com.im.common.confirm(msgStr,{},function(){
                var consList = [];
                $.each(nowlist,function (i,obj) {
                    consList.push(obj.constParamId);
                });
                var result = client.callAsJson("commonService/saveOrUpdateBusiParam", {consList : consList ,batchOperType : type});
                if(result.success){
                    toastr.info("操作成功！");
                    param.pageInfo.pageNumber = 1;
                    _queryBusiParamPage();
                }else{
                    toastr.error("服务器忙，请稍后再试！"+result.resultMsg);
                }
            });
        }
    }
    return {
        init: _init,
        modifyBusiParam : _modifyBusiParam,
        modifyBusiParamVal : _modifyBusiParamVal
    }

}();

$(function () {
    com.im.constantManage.invoke.init();
});